package com.tcenter.dao.providers.impl;

import com.tcenter.dao.entities.User;
import com.tcenter.dao.entities.UserRole;
import com.tcenter.dao.providers.UserDAO;
import org.hibernate.Query;
import org.springframework.context.annotation.Scope;
import org.springframework.stereotype.Repository;

import java.util.LinkedList;
import java.util.List;

/**
 * User: Andrey Pautov
 * Date: 5/26/13
 */
@Repository("userDAO")
@Scope("request")
public abstract class UserDAOImpl<T extends User> extends BaseDAOImpl<T> implements UserDAO<T> {

    private String tableName;


    @Override
    @SuppressWarnings("unchecked, JpaQlInspection")
    public T getByEmail(String email) {
        Query query = getCurrentSession().createQuery("from " + tableName + " where email=:email");
        query.setString("email", email);
        return (T) query.uniqueResult();
    }

    @SuppressWarnings("unchecked")
    public List<UserRole> getUserRolesById(Integer id) {
        User user = (User) getById(id);
        List<UserRole> roles = new LinkedList<UserRole>();
        roles.addAll(user.getUserRolesForUserId());
        return roles;
    }


    public void setTableName(String tableName) {
        this.tableName = tableName;
    }

}
